package org.osiris.server.servlets;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;


public class ImageDocumentServlet extends HttpServlet {



	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Logger logger=Logger.getLogger(this.getClass());
	@Resource
	//AfuptoolManager Mapper;
	protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
		logger.info("Starting image servlet for getRequestURL:"+httpServletRequest.getRequestURL());
		logger.info("Starting image servlet for getRequestURI:"+httpServletRequest.getRequestURI());
		String image_id = null;
		try{
			image_id=httpServletRequest.getParameter("file");

		}catch(Exception me){	    	  
			me.printStackTrace();
		}
		String image_seed = null;
		try{
			image_seed =httpServletRequest.getParameter("seed");

		}catch(Exception me){	    	  
			me.printStackTrace();
		}
		logger.info("Starting image servlet image_id:"+image_id);
		logger.info("Starting image servlet image_seed:"+image_seed);
		try{
			ApplicationContext ctx = null;//AppContext.getApplicationContext();

			
			String filename=null;
			
			try{
			if(image_id!=null){				
			//	filename=Mapper.getImageFile(image_id);
			}else if(image_seed!=null){
				try{
				if(!image_seed.trim().equalsIgnoreCase("0")){
					//	filename=SolicitudServiceImpl.busca(image_seed);
				}else {
					filename=SimplePictureFormUploader.pinturesPath+File.separator+"dnielectronico.JPG";
				}
				}catch(Exception mex){}
			}else {
				filename=SimplePictureFormUploader.pinturesPath+File.separator+"Perfil.JPG";				
			}
			}catch(Exception mex){
				filename=SimplePictureFormUploader.pinturesPath+File.separator+"Perfil.JPG";
			}
			logger.info("filename:"+filename);			
			httpServletResponse.setHeader("Cache-Control", "no-store");
			httpServletResponse.setHeader("Pragma", "no-cache");
			httpServletResponse.setDateHeader("Expires", 0);
			httpServletResponse.setContentType("image/jpeg");
			File file = new File(filename);
			httpServletResponse.setContentLength((int)file.length());
			FileInputStream in = new FileInputStream(file);
			ServletOutputStream out = httpServletResponse.getOutputStream();
			byte[] buf = new byte[1024];
			int count = 0;
			while ((count = in.read(buf)) >= 0) {
				out.write(buf, 0, count);
			}
			in.close();
			out.flush();
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
			httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			return;
		}

	}

}
